package com.fh.controller;

import com.fh.entity.Cart;
import com.fh.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.ResultObj;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {
    @Resource
    private CartService cartService;
    @Resource
    private HttpServletRequest request;

    @PostMapping
    public ResultObj addCart(@RequestBody Cart cart, String phone){
        phone = request.getHeader("phone");
        cart = cartService.addCart(cart, phone);
        return ResultObj.success(cart);

    }
    @GetMapping
    public  ResultObj getCount(){
        String phone = request.getHeader("phone");
        Long count= cartService.getCount(phone);
        return ResultObj.success(count);
    }
    @GetMapping("all")
    public ResultObj getCart(){
        String phone = request.getHeader("phone");
        List<Cart> cartList=cartService.getCar(phone);
        return ResultObj.success(cartList);
    }
    @PostMapping("count")
    public ResultObj updateProductCount(@RequestBody Cart cart){
        String phone = request.getHeader("phone");
        cartService.updateProductCount(cart,phone);
        return ResultObj.success();
    }
    @DeleteMapping("{skuCodes}")
    public ResultObj deleteBatch(@PathVariable("skuCodes") String skuCodes){
        String phone = request.getHeader("phone");
        String[] split = skuCodes.split(",");
        for (int i = 0; i < split.length; i++) {
            cartService.deleteBatch(split[i],phone);
        }
        return ResultObj.success();

    }


}
